package com.huowenjie.service;

import com.huowenjie.dao.UserDao;
import com.huowenjie.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


@Service
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired
    UserDao userDao;




    @Override
    public Page<User> userlist(User user, Integer pageNum, Integer pageSize) {

        PageRequest of = PageRequest.of(pageNum, pageSize);
        Page<User> all = userDao.findAll(of);

        System.out.println(all);

        return all;
    }

    @Override
    public boolean dell(String[] ids) {

        for (int i = 0; i < ids.length; i++) {
            String id = ids[i];

            userDao.deleteById(Integer.parseInt(id));
        }

        return true;
    }

    @Override
    public void add(User user) {
        if(user.getId()==null){
            userDao.save(user);
        }else {
            userDao.saveAndFlush(user);
        }
    }


    @Override
    public User look(Integer id) {
        Optional<User> byId = userDao.findById(id);
        User user = byId.get();
        return user;
    }
}
